package com.wonderful.compose.navigation

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.wonderful.compose.ui.theme.ComposeLearnTheme

/**
 * Describe: 使用 navigation 跳转导航
 *
 * @author Wonderful
 * @time 2025/9/16 10:19
 */
class UseNavigationNavActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeLearnTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    UseNavigationNav()
                }
            }
        }
    }
}

const val PAGE_A = "page_a"
const val PAGE_B = "page_b"
const val PAGE_C = "page_c"

@Composable
fun UseNavigationNav(modifier: Modifier = Modifier) {
    val navController = rememberNavController()

    // 获取带状态的路由
    val currentBackStack by navController.currentBackStackEntryAsState()
    if (currentBackStack?.destination?.route == PAGE_C) {
        Toast.makeText(LocalContext.current, "You are in Screen C", Toast.LENGTH_SHORT).show()
    }


    // 方式一：统一定义 onClick，但不一定可靠，因为 navController.currentDestination?.route 不一定是你想要的
    val onClick = {
        // 根据当前页面的路由值，判断下一个要跳转的页面
        Log.d("UseNavigationNav", "onClick: ${navController.currentDestination?.route}")
        when (navController.currentDestination?.route) {
            PAGE_A -> {
                val name = "Wonderful"
                val age = 20
                navController.navigate("${PAGE_B}/$name/$age")
            }

            PAGE_B -> {
                navController.navigate(PAGE_C)
            }
            PAGE_C -> navController.navigate(PAGE_A) {
                popUpTo(PAGE_A) {
                    inclusive = true
                }
            }
        }
    }

    // 方式二：不依赖currentDestination
    val toPageB = {
        val name = "Wonderful"
        val age = 20
        navController.navigate("${PAGE_B}/$name/$age")
    }

    val toPageC = {
        navController.navigate(PAGE_C)
    }

    val toPageA = {
        navController.navigate(PAGE_A) {
            popUpTo(PAGE_A) {
                inclusive = true
            }
        }
    }

    NavHost(navController = navController, startDestination = PAGE_A, modifier = modifier) {
        composable(route = PAGE_A) {
            ScreenA(onClick = onClick)
        }
        composable(
            route = "${PAGE_B}/{name}/{age}",
            arguments = listOf(
                navArgument("name") {
                    type = NavType.StringType
                },
                navArgument("age") {
                    type = NavType.IntType
                },
            )
        ) { navBackStackEntry ->
            val name = navBackStackEntry.arguments?.getString("name")
            val age = navBackStackEntry.arguments?.getInt("age")
            //ScreenB(onClick = onClick, name = name, age = age)
            ScreenB(onClick = toPageC, name = name, age = age)
        }
        composable(route = PAGE_C) {
            ScreenC(onClick = onClick)
//            ScreenC {
//                navController.navigate(PAGE_A)
//            }
        }
    }
}